<?php

namespace app\api\service\store;

use app\api\model\store\Cart as CartModel;
use app\api\service\BaseService;

class Cart extends BaseService
{

    public $model = NULL;

    public function __construct()
    {
        $this->model = new CartModel();
    }

    /**
     * @param $userId
     * @param $ids
     * @param $isAddOrder true添加到订单表
     * @return array
     */
    public function lists($userId, $ids, $isAddOrder = false, $is_new = 0){
        try {
            $where['user_id'] = $userId;
            $where['status'] = 1;
            $where['is_new'] = $is_new;
            if(!empty($ids)) {
                $where['id'] = ['in', $ids];
            }

            $cartList = $this->model->getList($where,'*', ['sku']);
        }catch (\Exception $e) {
            $cartList = [];
        }
        if(!$cartList) {
            return [];
        }
        //return $cartList;
        $list = [];
        foreach ($cartList as $item){
            $price = $item['sku']['sell_price'];
            $v['name'] = $item['name'];
            if($isAddOrder == true){
                $v['goods_id'] = $item['goods_id'];
                $v['sku_id'] = $item['sku_id'];
                $v['origin_image'] = $item['origin_image'];
            }else{
                $v['id'] = $item['id'];
                $v['image'] = $item['image'];
            }
            $v['price'] = $price;
            $v['num'] = $item['num'];
            $v['total_price'] = $price * $item['num'];
            $v['props_text'] = $item['props_text'];
            //$v['sku'] = $specsValues[$k] ?? "暂无规则";
            $list[] = $v;
        }

        return $list;
    }

    public function delete($userId, $ids){
        try {
            $where['user_id'] = $userId;
            $where['id'] = ['in', $ids];
            $res = $this->model->delDataByWhere($where);
        }catch (\Exception $e) {
            return FALSE;
        }
        return $res;
    }


}